package com.anket;

import java.awt.Font;
import java.io.File;
import java.io.FileInputStream;
import java.util.logging.Logger;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.PieDataset;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

@ManagedBean
public class DynamicImageController {

	private StreamedContent barcode;  
	  
    private StreamedContent chart;  
  
    private static final Logger logger = Logger.getLogger(DynamicImageController.class.getName());  
  
    public DynamicImageController() {  
        //Chart  
       
 
    }  
  
    public StreamedContent getBarcode() {  
        return barcode;  
    }  
    public void setBarcode(StreamedContent barcode) {  
        this.barcode = barcode;  
    }  
  
  
    public StreamedContent getChart() {  
        return chart;  
    }  
    public void setChart(StreamedContent chart) {  
        this.chart = chart;  
    }  
  
    
    public StreamedContent print(PieDataset dataset){
    	 try {  
             JFreeChart jfreechart = ChartFactory.createPieChart("Turkish Cities", dataset, true, true, false);  
             File chartFile = new File("dynamichart");  
             
             
             final PiePlot plot = (PiePlot) jfreechart.getPlot();
             plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
             plot.setNoDataMessage("No data available");
             plot.setCircular(false);
             plot.setLabelGap(0.02);
             
             
             ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 375, 300);  
             chart = new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");
             
             
             
             
             
             
         } catch (Exception e) {  
             logger.severe(e.getMessage());  
         }

         return chart;
    }
}  
